package com.cls.business.controller;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.cls.common.annotation.ControllerEndpoint;
import com.cls.common.utils.MyUtil;
import com.cls.common.entity.MyConstant;
import com.cls.common.controller.BaseController;
import com.cls.common.entity.QueryRequest;
import com.cls.common.entity.ResponseVO;
import com.github.pagehelper.PageInfo;
import com.cls.business.entity.LeaderProject;
import com.cls.business.service.ILeaderProjectService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PathVariable;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 主持人研究项目情况表 Controller
 *
 * @author wandering
 * @date 2020-11-12 15:25:57
 */
@Slf4j
@Validated
@Controller
@RequiredArgsConstructor
public class LeaderProjectController extends BaseController {

    private final ILeaderProjectService leaderProjectService;

    /**
    * 主持人研究项目情况表列表页面
    **/
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/leaderProject")
    public String leaderProjectIndex(){
        return MyUtil.view("business/leaderProject/leaderProject");
    }

    /**
    * 主持人研究项目情况表新增页面
    * @return
    */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/leaderProject/leaderProjectAdd")
    public String leaderProjectAdd(Model model){
        return MyUtil.view("business/leaderProject/leaderProjectAdd");
    }

    /**
    * 主持人研究项目情况表修改页面
    * @return
    */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/leaderProject/leaderProjectUpdate/{leaderProjectId}")
    public String leaderProjectUpdate(@PathVariable Long leaderProjectId, Model model){
        LeaderProject leaderProject = leaderProjectService.getById(leaderProjectId);
        model.addAttribute("leaderProject",leaderProject);
        return MyUtil.view("business/leaderProject/leaderProjectUpdate");
    }



    /**
    * 主持人研究项目情况表详情页面
    * @return
    */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/leaderProject/leaderProjectDetail/{leaderProjectId}")
    public String leaderProjectDetail(@PathVariable Long leaderProjectId, Model model){
        LeaderProject leaderProject = leaderProjectService.getById(leaderProjectId);
        model.addAttribute("leaderProject",leaderProject);
        return MyUtil.view("business/leaderProject/leaderProjectDetail");
    }

    @GetMapping("leaderProject")
    @ResponseBody
    @RequiresPermissions("leaderProject:list")
    public ResponseVO getAllLeaderProjects(LeaderProject leaderProject) {
        return ResponseVO.success(leaderProjectService.findLeaderProjects(leaderProject));
    }

    @GetMapping("{declareId}/leaderProject/list")
    @ResponseBody
//    @RequiresPermissions("leaderProject:list")
    public ResponseVO leaderProjectList(QueryRequest request, @PathVariable long declareId) {
        LeaderProject leaderProject = new LeaderProject();
        leaderProject.setDeclareId(declareId);
        PageInfo<LeaderProject> pageInfo =  this.leaderProjectService.findLeaderProjects(request, leaderProject);
        return ResponseVO.success(pageInfo);
    }
    @GetMapping("{declareId}/leaderProject/listAll")
    @ResponseBody
    public ResponseVO leaderProjectLists(@PathVariable long declareId) {
        LeaderProject leaderProject = new LeaderProject();
        leaderProject.setDeclareId(declareId);
        List<LeaderProject> list =  this.leaderProjectService.findLeaderProjects(leaderProject);
        return ResponseVO.success(list);
    }

    @ControllerEndpoint(operation = "新增LeaderProject", exceptionMessage = "新增LeaderProject失败")
    @PostMapping("leaderProject")
    @ResponseBody
    @RequiresPermissions("leaderProject:add")
    public ResponseVO addLeaderProject(@Valid LeaderProject leaderProject) {
        this.leaderProjectService.createLeaderProject(leaderProject);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "多个删除LeaderProject", exceptionMessage = "删除LeaderProject失败")
    @PostMapping("leaderProject/deletes/{ids}")
    @ResponseBody
    @RequiresPermissions("leaderProject:delete")
    public ResponseVO deleteLeaderProjects(@PathVariable String ids) {
        String[] idArry = ids.split(StringPool.COMMA);
        this.leaderProjectService.deleteLeaderProjects(idArry);
        return ResponseVO.success();
    }


    @ControllerEndpoint(operation = "删除单个LeaderProject", exceptionMessage = "删除LeaderProject失败")
    @PostMapping("leaderProject/delete/{leaderProjectId}")
    @ResponseBody
    @RequiresPermissions("leaderProject:delete")
    public ResponseVO deleteLeaderProject(@PathVariable Long leaderProjectId) {
        this.leaderProjectService.removeById(leaderProjectId);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "修改LeaderProject", exceptionMessage = "修改LeaderProject失败")
    @PostMapping("leaderProject/update")
    @ResponseBody
    @RequiresPermissions("leaderProject:update")
    public ResponseVO updateLeaderProject(LeaderProject leaderProject) {
        this.leaderProjectService.updateLeaderProject(leaderProject);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "修改LeaderProject", exceptionMessage = "导出Excel失败")
    @PostMapping("leaderProject/excel")
    @ResponseBody
    @RequiresPermissions("leaderProject:export")
    public void export(QueryRequest queryRequest, LeaderProject leaderProject, HttpServletResponse response) {
        List<LeaderProject> leaderProjects = this.leaderProjectService.findLeaderProjects(queryRequest, leaderProject).getList();
        ExcelKit.$Export(LeaderProject.class, response).downXlsx(leaderProjects, false);
    }
}
